#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/8 11:26
# @Author  : Andrewq
# @Site    : nope
# @File    : newspaperResource.py
# @Software: PyCharm

from flask import jsonify, request

from ..models.NewspaperInfoModel import NewspaperInfoManager
from ..utils.response_code import RET


class NewspaperResource():

    @classmethod
    def get_newspaper_all_info(cls):

        result_dict = NewspaperInfoManager.get_newspaper_all_info()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def add_newspaper_info(cls):

        newspaper_name = request.form.get('NewspaperName')
        newspaper_price = request.form.get('Price')
        newspaper_other_info = request.form.get('OtherInfo')

        if not all([newspaper_name, newspaper_price, newspaper_other_info]):
            return jsonify(code=RET.PARAMERR, msg='newspaper_name, newspaper_price, newspaper_other_info', data=[])

        kwargs = {
            'NewspaperName': newspaper_name,
            'Price': newspaper_price,
            'OtherInfo': newspaper_other_info
        }

        result_dict = NewspaperInfoManager.add_newspaper_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_newspaper_info(cls):
        pass

    @classmethod
    def delete_newspaper_info_list(cls):
        data = eval(request.form.get('data'))

        if not data:
            return jsonify(code=RET.PARAMERR, msg='缺失参数data', data=[])

        result_dict = NewspaperInfoManager.delete_newspaper_info_list(**{'data': data})

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def edit_newspaper_info(cls):
        newspaper_ID = request.form.get('NewspaperID')
        newspaper_name = request.form.get('NewspaperName')
        newspaper_price = request.form.get('Price')
        newspaper_info = request.form.get('OtherInfo')

        if not all([newspaper_ID, newspaper_name, newspaper_price, newspaper_info]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数newspaper_ID, newspaper_name, newspaper_price, newspaper_info', data=[])

        kwargs = {
            'NewspaperID': newspaper_ID,
            'NewspaperName': newspaper_name,
            'Price': newspaper_price,
            'OtherInfo': newspaper_info
        }

        result_dict = NewspaperInfoManager.edit_newspaper_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_newspaper_info_by_tags(cls):
        tag_name = request.args.get('tag')
        keyword = request.args.get('keyword')

        if all([tag_name, keyword]):
            args = {
                'tag': tag_name,
                'keyword': keyword
            }
        else:
            args = {}

        result_dict = NewspaperInfoManager.get_order_info_by_tags(**args)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_newspaper_info(cls):
        newspaper_ID = request.form.get('NewspaperID')

        if not newspaper_ID:
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_ID', data=[])

        result_dict = NewspaperInfoManager.delete_newspaper_info(**{'NewspaperID': newspaper_ID})

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))